package cn.huobi.test;

import cn.huobi.utils.ESClientUtil;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

public class EsTest_02 {

    @Test
    public void testAdd() throws Exception{
        TransportClient client = ESClientUtil.getClient();
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", 1);
        map.put("name","佘导");
        map.put("age", "8");
        map.put("sex", "1");

        IndexResponse indexResponse = client.prepareIndex("pethome", "employee", "1").setSource(map).get();
        System.out.println(indexResponse);
    }

    @Test
    public void testGet() throws Exception{
        TransportClient client = ESClientUtil.getClient();
        GetResponse documentFields = client.prepareGet("pethome", "employee", "1").get();
        System.out.println(documentFields.getSourceAsMap());
    }

    @Test
    public void testUpdate() throws Exception{
        TransportClient client = ESClientUtil.getClient();
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","老毕");
        UpdateResponse updateResponse = client.prepareUpdate("pethome", "employee", "1").setDoc(map).get();
        System.out.println(updateResponse);
    }

    @Test
    public void testDelete() throws Exception{
        TransportClient client = ESClientUtil.getClient();
        DeleteResponse deleteResponse = client.prepareDelete("pethome", "employee", "1").get();
        System.out.println(deleteResponse);
    }

    @Test
    public void testBatchAdd() throws Exception{
        TransportClient client = ESClientUtil.getClient();
        for (int i = 0; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "employee", i + "");
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", i);
            if(i % 3 == 0){
                map.put("name", "老吕喜欢教java之"+i);
            }else if (i % 3 == 1){
                map.put("name", "老毕爱演java之"+i);
            }else {
                map.put("name", "佘导拍java之"+i);
            }
            map.put("age", i);
            map.put("sex", i%2);
            IndexResponse indexResponse = builder.setSource(map).get();

            System.out.println(indexResponse);
        }

    }

    @Test
    public void testComplex() throws Exception{
        TransportClient client = ESClientUtil.getClient();
        SearchRequestBuilder pethome = client.prepareSearch("pethome").setTypes("employee");
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.matchQuery("name", "java"))
                .filter(QueryBuilders.termQuery("sex", 1))
                .filter(QueryBuilders.rangeQuery("age").gte(1).lte(24));

        pethome.setQuery(boolQuery);
        pethome.setSize(20);
        pethome.setFrom(0);
        pethome.addSort("age", SortOrder.DESC);
        SearchResponse searchResponse = pethome.get();
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }

    }
}
